package cn.seaboot.admin.quartz.web;

import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.quartz.bean.ScheduleJob;
import cn.seaboot.admin.quartz.service.ScheduleJobService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 计划任务 [t_schedule_job] - Controller
 * <p>
 *
 * @author Mr.css on 2018-6-20
 */
@Controller
@RequestMapping("schedule/job")
@Tag(name = "计划任务 [t_schedule_job] 管理")
public class ScheduleJobCtrl {

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 删
     *
     * @param id ID
     * @return affected rows
     */
    @ResponseBody
    @Parameter(name = "id", description = "ID")
    @Operation(description = "计划任务 [t_schedule_job] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params Long id) throws SchedulerException {
        return scheduleJobService.deleteById(id);
    }

    /**
     * 查
     *
     * @param id ID
     * @return pojo
     */
    @ResponseBody
    @Parameter(name = "id", description = "ID")
    @Operation(description = "计划任务 [t_schedule_job] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public ScheduleJob queryById(@Params Long id) {
        return scheduleJobService.queryById(id);
    }

    /**
     * 改
     *
     * @param id ID
     */
    @ResponseBody
    @Parameter(name = "id", description = "ID")
    @Operation(description = "计划任务 [t_schedule_job] 启用任务")
    @RequestMapping(value = "resume", method = RequestMethod.PUT)
    public int turnOn(@Params Long id) throws SchedulerException {
        return scheduleJobService.resumeJob(id);
    }

    /**
     * 改
     *
     * @param id ID
     */
    @ResponseBody
    @Parameter(name = "id", description = "ID")
    @Operation(description = "计划任务 [t_schedule_job] 禁用任务")
    @RequestMapping(value = "pause", method = RequestMethod.PUT)
    public int turnOff(@Params Long id) throws SchedulerException {
        return scheduleJobService.disableJob(id);
    }

    /**
     * 获取下一次触发时间
     *
     * @param cron 时间设置，参考quartz说明文档
     */
    @ResponseBody
    @Operation(description = "计划任务 [t_schedule_job] 获取下一次触发时间")
    @Parameters({
            @Parameter(name = "cron", description = "cron表达式"),
            @Parameter(name = "times", description = "时间数量")
    })
    @RequestMapping(value = "nextFireTime", method = RequestMethod.GET)
    public List<Date> nextFire(String cron, @RequestParam(defaultValue = "5") Integer times) {
        return scheduleJobService.getNextFireTime(cron, times);
    }

    /**
     * 改
     *
     * @param id   ID
     * @param cron 时间设置，参考quartz说明文档
     */
    @ResponseBody
    @Operation(description = "计划任务 [t_schedule_job] 改")
    @Parameters({
            @Parameter(name = "id", description = "ID"),
            @Parameter(name = "cron", description = "cron表达式")
    })
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Params Long id, @Params String cron) throws SchedulerException {
        return scheduleJobService.modifyJobTime(id, cron);
    }

    /**
     * 立即触发1次计划任务
     *
     * @param id   ID
     * @return pojo
     */
    @ResponseBody
    @Parameter(name = "id", description = "ID")
    @Operation(description = "立即触发1次计划任务")
    @RequestMapping(value = "trigger", method = RequestMethod.GET)
    public Result triggerJob(@Params Long id) throws SchedulerException {
        scheduleJobService.triggerJob(id);
        return Result.succeed();
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "计划任务 [t_schedule_job] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<ScheduleJob> queryList(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map -> scheduleJobService.queryList(map));
    }
}
